﻿using NoteSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace NoteSystem
{
    public partial class Info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //页面首次加载机制：你要不关，你的值修改不了
            if (!IsPostBack)
            {
                InitProvince();
                if (Session["User"] != null)
                {
                    if (Request["id"] != null)
                    {
                        //1)接收传递的id
                        //2)根据id查询数据库中的数据
                        //3)把查到的数据赋给文本框组件
                        int id = Convert.ToInt32(Request["id"]);

                        //select n.*,u.account from dbo.notes n inner join users u on u.id = n.user_id
                        string sql = string.Format("select u.*,d.CityID,c.ProvinceID from users u " +
                            "inner join S_District d on d.DistrictID=u.district_id inner join S_City c on c.CityID=d.CityID inner join " +
                            "S_Province p on p.ProvinceID=c.ProvinceID where id="+id);
                        DataSet ds = DbHelper.Query(sql);
                        txtAccount.Text = ds.Tables[0].Rows[0]["account"].ToString();
                        txtBirthday.Text = ds.Tables[0].Rows[0]["birthday"].ToString();
                        //下拉框
                        ddlProvince.SelectedValue = ds.Tables[0].Rows[0]["ProvinceID"].ToString();
                        int ProvinceID =Convert.ToInt32(ddlProvince.SelectedValue);
                        InitCity(ProvinceID);
                        ddlCity.SelectedValue = ds.Tables[0].Rows[0]["CityID"].ToString();
                        int CityID = Convert.ToInt32(ddlCity.SelectedValue);
                        InitDistrict(CityID);
                        ddlDistrict.SelectedValue = ds.Tables[0].Rows[0]["district_id"].ToString();
                        rbtnGender.SelectedValue = ds.Tables[0].Rows[0]["gender"].ToString();
                        Image1.ImageUrl = ds.Tables[0].Rows[0]["img_url"].ToString();
                        txtPhone.Text = ds.Tables[0].Rows[0]["phone"].ToString();
                        txtEmail.Text = ds.Tables[0].Rows[0]["email"].ToString();
                        txtQQ.Text = ds.Tables[0].Rows[0]["qq"].ToString();
                    }
                    else
                    {
                        //个人修改
                        DataRow userRow = (DataRow)Session["User"];
                        int id = Convert.ToInt32(userRow["id"]);
                        string sql = string.Format("select d.CityID,c.ProvinceID from users u " +
                            "inner join S_District d on d.DistrictID=u.district_id inner join S_City c on c.CityID=d.CityID inner join " +
                            "S_Province p on p.ProvinceID=c.ProvinceID where id=" + id);
                        DataSet ds = DbHelper.Query(sql);
                        txtAccount.Text = userRow["account"].ToString();
                        txtBirthday.Text = userRow["birthday"].ToString();
                        //下拉框
                        ddlProvince.SelectedValue = ds.Tables[0].Rows[0]["ProvinceID"].ToString();
                        int ProvinceID = Convert.ToInt32(ddlProvince.SelectedValue);
                        InitCity(ProvinceID);
                        ddlCity.SelectedValue = ds.Tables[0].Rows[0]["CityID"].ToString();
                        int CityID = Convert.ToInt32(ddlCity.SelectedValue);
                        InitDistrict(CityID);
                        ddlDistrict.SelectedValue = userRow["district_id"].ToString();
                        rbtnGender.SelectedValue = userRow["gender"].ToString();
                        Image1.ImageUrl = userRow["img_url"].ToString();
                        txtPhone.Text = userRow["phone"].ToString();
                        txtEmail.Text = userRow["email"].ToString();
                        txtQQ.Text = userRow["qq"].ToString();
                    }                
                }
                else
                {
                    Response.Write("<script>alert('请先登录!');window.location.href='Login.aspx'</script>");
                }
                
            }
        }
        private void InitProvince()
        {
            // 从数据库或其他数据源获取省份数据
            DataSet ds = DbHelper.Query("select * from S_Province"); // 伪代码
            ddlProvince.DataSource = ds;
            ddlProvince.DataTextField = "ProvinceName";
            ddlProvince.DataValueField = "ProvinceID";
            ddlProvince.DataBind();
            ddlProvince.Items.Insert(0, new ListItem("请选择省份", "0"));
        }
        public void InitCity(int ProvinceID)
        {
            // 从数据库或其他数据源获取城市数据
            DataSet ds = DbHelper.Query("select * from S_City where ProvinceID=" + ProvinceID);
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();
            ddlCity.Items.Insert(0, new ListItem("请选择城市", "0"));
        }

        private void InitDistrict(int CityID)
        {
            // 从数据库或其他数据源获取区域数据
            DataSet ds = DbHelper.Query("select * from S_District where CityID=" + CityID); // 伪代码
            ddlDistrict.DataSource = ds;
            ddlDistrict.DataTextField = "DistrictName";
            ddlDistrict.DataValueField = "DistrictID";
            ddlDistrict.DataBind();
            ddlDistrict.Items.Insert(0, new ListItem("请选择区域", "0"));
        }
        protected void btn1_Click(object sender, EventArgs e)
        {
            DataRow userRow = (DataRow)Session["User"];
            //获取用户插入的有效信息
            string account = txtAccount.Text.Trim();
            string birthday = txtBirthday.Text.Trim();
            int cityId = Convert.ToInt32(ddlCity.SelectedValue);//获取下拉框的选中值
            int gender = Convert.ToInt32(rbtnGender.SelectedValue);
            string imgUrl = Image1.ImageUrl;//获取图片框的网络地址
            string phone = txtPhone.Text.Trim();
            string email = txtEmail.Text.Trim();
            string qq = txtQQ.Text.Trim();
            string pwd = userRow["pwd"].ToString();
            //数据库插入
            string sql = string.Format(@"insert into users values('{0}','{1}','{2}',{3},{4},'{5}','{6}','{7}','{8}',0,0,'{9}',0)", account, birthday, pwd, cityId, gender, imgUrl, phone, email, qq, DateTime.Now);
            //执行更新操作
            int flag = DbHelper.ExecuteSql(sql);
            if (flag > 0)
            {
                Response.Write("<script>alert('修改成功')</script>");
            }
        }
        //头像上传
        protected void Button1_Click(object sender, EventArgs e)
        {
            string exList = ".jpg.png.gif";
            // \反斜杠:1)转义   2)路径深度（物理）
            // /正斜杠:1)表示网络路径深度
            // 网络路径：网站中使用的路径  图片加载 文件的下载
            // 物理路径：计算机中磁盘路径  本地计算机中硬盘
            //1.捕获文件名称
            string imgName = fulImg.FileName;//a.jpg
            string exName = imgName.Substring(imgName.LastIndexOf('.'));
            if (!exList.Contains(exName))
            {
                Response.Write("<script>alert('支持上传图片格式！')</script>");
                return;
            }
            if (fulImg.FileContent.Length > 2 * 1024 * 1024)
            {
                Response.Write("<script>alert('图片大小不能超过2M！')</script>");
                return;
            }
            //2.确定文件保存的物理地址
            string path = Server.MapPath("images") + "\\" + imgName;
            //3.进行文件保存了
            fulImg.SaveAs(path);
            Image1.ImageUrl = "images/" + imgName;
        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCity.Items.Clear();
            if (ddlProvince.SelectedValue != "")
            {
                int ProvinceID = Convert.ToInt32(ddlProvince.SelectedValue);
                InitCity(ProvinceID);
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDistrict.Items.Clear();
            if (ddlCity.SelectedValue != "")
            {
                int CityID = Convert.ToInt32(ddlCity.SelectedValue);
                InitDistrict(CityID);
            }
        }
    }
}